package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.DutyBean;
import com.ray.bean.TableData;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.service.OrderService;
import com.ray.service.ReturnWorkService;
import com.ray.service.WorkshopService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;

@Controller
@RequestMapping("/returnRepair")
public class ReturnRepairController {
	@Autowired
	ReturnWorkService returnWorkService;
	@Autowired
	OrderService orderService;
	@Autowired
	WorkshopService workshopService;
	
    //获取订单详情
	@RequestMapping("/getOrderDetail")
	@ResponseBody
	public Map getOrderDetail(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getOrderDetail(map));
	}
	
	//查询产能信息,根据车间与工序
	@RequestMapping("/getTotal")
	@ResponseBody
	public Map getTotal(String date,String startDay,HttpServletRequest request) throws ServiceException, ParseException{
		MyUser myUser=RequestUtil.getLoginUser(request);
		return ResponseUtil.ResponseResult(returnWorkService.getTotal(date,startDay,myUser));
	}
	
    //查询产能信息,根据车间与时间
	@RequestMapping("/getTableData")
	@ResponseBody
	public DatatablesViewPageBean getTableData(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return returnWorkService.getTableData(map);
	}
	
	//人员产能信息
	@RequestMapping("/getPersonData")
	@ResponseBody
	public Map getPersonData(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		 return ResponseUtil.ResponseResult(returnWorkService.getPersonData(map));
	}
	
	
	//查询每个车间拖期订单
	@RequestMapping("/getDelayDate")
	@ResponseBody
	public DatatablesViewPageBean<DutyBean> getDelayDate(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return returnWorkService.getDelayDate(map);
	}
	
	//查询每个车间七天内拖期订单数
	@RequestMapping("/getSoonDelay")
	@ResponseBody
	public Map getSoonDelay(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.getSoonDelay(map));
	}
	
	//查询每个车间拖期订单
	@RequestMapping("/getOnTime")
	@ResponseBody
	public Map getOnTime(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.getOnTime(map));
	}
	
	//查询每个车间七天内拖期订单数
		@RequestMapping("/getSereven")
		@ResponseBody
		public Map getSereven(HttpServletRequest request) throws ServiceException{
			Map map=RequestUtil.getRequestAllParam(request);
			return ResponseUtil.ResponseResult(returnWorkService.getSereven(map));
		}
	
	
	//查询每个车间周转量
	@RequestMapping("/getDeptTurnover")
	@ResponseBody
	public Map getDeptTurnover(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.getDeptTurnover(map));
	}
	
	
	//获取周转量数据
	@RequestMapping("/getTrunover")
	@ResponseBody
	public Map getTrunover(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.getTrunover(map));
	}
	
	
	//获取整厂周转量
	@RequestMapping("/getFactoryTurnover")
	@ResponseBody
	public Map getFactoryTurnover(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.getFactoryTurnover(map));
	}
	
	
	//获取订单生产情况
	@RequestMapping("/getOrderWork")
	@ResponseBody
	public Map getOrderWork(HttpServletRequest request) throws ServiceException {
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.getOrderWork(map));
	}
	
	//查询产能信息,根据车间与工序
	@RequestMapping("/queryCapacity")
	@ResponseBody
	public Map queryCapacity(String workshopName,String workingProcedureName,String listString){
		return ResponseUtil.ResponseResult(returnWorkService.queryCapacity(workshopName,workingProcedureName,listString));
	}
	
	//查询车间七日周转量
	@RequestMapping("/serverTurnover")
	@ResponseBody
	public Map serverTurnover(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.serverTurnover(map));
	}
	
	
    //获取所有车间名称
	@RequestMapping("/getWorkshopName")
	@ResponseBody
	public Map getWorkshopName(HttpServletRequest request) throws ServiceException{
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(workshopService.getWorkshopName(map));
	}
	
//   //	获取该车间下所有工序名称
//	@RequestMapping("/getWorkingProcedureName")
//	@ResponseBody
//	public Map getWorkingProcedureName(HttpServletRequest request) throws ServiceException{
//		Map map=RequestUtil.getRequestAllParam(request);
//		return ResponseUtil.ResponseResult(workshopService.getWorkingProcedureName(map));
//	}
	
	//获取部门报工加工对象
	@RequestMapping("/getMachining")
	@ResponseBody
	public Map getMachining(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(returnWorkService.getMachining(map));
	}
	
	
	//下载员工达成率数据
		@RequestMapping(value="/downlodeReachFile",method=RequestMethod.GET)
		@ResponseBody
		public void downlodeReachFile(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
			Map map=RequestUtil.getRequestAllParam(request);
			map.put("start", null);
			map.put("length", null);
			DatatablesViewPageBean<TableData> datatablesViewPageBean=returnWorkService.getTableData(map);
			List<TableData> tableList=datatablesViewPageBean.getReturnData();
			String [] tableHeadFields={"姓名","作业工序","作业产能","作业单付时间(秒)","有效工作时间(时)","工作效率","报工日期","单价","总价"};
			String [] fields={"name","procedur","capacity","oneTime","effectiveTime","workEffective","workDate","price","sumPrice"};
			String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
			File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
			ResponseUtil.downloadfile(response, excelFile, "员工达成率.xlsx", true);
		}
	
	

		//下载车间拖期数据
		@RequestMapping(value="/downlodeDalay",method=RequestMethod.GET)
		@ResponseBody
		public void downlodeDalay(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
			Map map=RequestUtil.getRequestAllParam(request);
			map.put("start", null);
			map.put("length", null);
			DatatablesViewPageBean<DutyBean> datatablesViewPageBean=returnWorkService.getDelayDate(map);
			List<DutyBean> tableList=datatablesViewPageBean.getReturnData();
			String [] tableHeadFields={"订单型号","客户型号","工厂型号","颜色描述","订单数量","交接入库时间","有效出库时间","拖期时间","产品良品数"};
			String [] fields={"orderId","customerId","factoryId","color","orderCount","inWorkShopTime","outWorkShopTime","delayTime","qualifiedCount"};
			String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
			File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
			ResponseUtil.downloadfile(response, excelFile, "拖期.xlsx", true);
		}
	
//		//下载车间周转量数据
//		@RequestMapping(value="/downlodeTurnover",method=RequestMethod.GET)
//		@ResponseBody
//		public void downlodeTurnover(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
//		    Map map =RequestUtil.getRequestAllParam(request);
//			String deptName=map.get("workShopName").toString();
//		    List<String> titleLists=workshopService.getWorkingProcedureName(map);
//			List<Map> tableList=returnWorkService.getTrunover(map);
//			String [] tableHeadFields={"订单编号","工厂型号","客户型号","客户订单号"};
//			String [] fields={"orderId","factoryId","keHu","keHuOrder"};
//			tableHeadFields=workshopService.getArrayHead(tableHeadFields,titleLists,"框未完成","框不良品","腿未完成","腿不良品","报工时间");
//			fields=workshopService.getArrayHead(fields,titleLists,"kuangTurnover","kuangNoQualified","tuiTurnover","tuiNoQualified","lastTime");
//			String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
//			File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
//			ResponseUtil.downloadfile(response, excelFile, "周转量.xlsx", true);
//		}


}
